package com.softserve.crazymachine.request;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Mykhailo Tys
 *
 */
public class Request {
	private String requestType;
	private String root;
	private String resource;
	private Map headers;
	private String contentType;
	
	public Request() {
		requestType = " ";
		root = " ";
		resource = " ";
		headers = new HashMap<String,String>();
	}
	
	public Request(String reqType, String r, String res, Map h, String contType) {
		requestType = reqType;
		root = r;
		resource = res;
		headers = new HashMap<String, String>();
		contentType = contType;
	}
	
	public void setResource(String value) {
		resource = value;
	}
	
	public void setRequestType(String reqType) {
		requestType = reqType;
	}
	
	public void setHeadersMap(Map map) {
		headers = map;
	}
	
	
	/**
	 * @param param request parameter name
	 * @return value of one of header parameters of http request
	 */
	public String getHeaderParameter(Object param) {
		return (String) headers.get((String)param);
	}
	
	
	/**
	 * @return parameters of POST http request
	 */
	public String getParametersForPost() {
		String parameters = " ";
		StringBuilder builder;
		if(requestType.equals("POST")) {
			builder = new StringBuilder((String)headers.get("Parameters:"));
			builder.delete(0, 1);
			parameters = builder.toString();
		}else{
			System.out.println("It is not POST request");
		}
		return parameters;
		
	}
	
	public String getRequestType() {
		return requestType;
	}
	
	public String toString() {
		return requestType + "\n" + root + "\n" + resource + "\n" + contentType;
	}
}
